<?
class Philomousia {
	
	/*********************************************************************************************************
	 *                                                PEOPLE
	 *********************************************************************************************************/
	/**
	 * Load all people from DB in an array of People objects
	 */
	public function loadPeople($handler) {
		$people = array();
		$query = "SELECT * FROM PEOPLE ORDER BY name ASC";
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadPeople()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$people[] = new People(
					$row['id'],
					$row['name'],
					$row['surname'],
					$row['description'],
					$row['mail'],
					$row['img'],
					$row['url'],
					$row['location']
				);
			}
		}
		return $people;
	}
	
	/**
	 * Insert a new person in DB
	 */
	public function insertPerson($name, $surname, $description, $mail, $img, $url, $location, $handler) {
		$name = $handler->cleanString($name);
		$surname = $handler->cleanString($surname);
		$description = $handler->cleanString($description);
		$mail = $handler->cleanString($mail);
		$img = $handler->cleanString($img);
		$img = ($img=="")? $handler->getBaseURL().'style/img/default.jpg' : $img;
		$url = $handler->cleanString($url);
		$location = $handler->cleanString($location);
		
		$query = "INSERT INTO PEOPLE(name, surname, description, mail, img, url, location)".
			" VALUES('$name', '$surname', '$description', '$mail', '$img', '$url', '$location')";
		if(($result = mysql_query($query))) {
			$handler->writeLog("[PANEL] ".strftime("%c")." person added by ".
				$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
		} else {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::addPerson()");
		}
		return $result;
	}
	
	
	/*********************************************************************************************************
	 *                                                COMMENT
	 *********************************************************************************************************/
	/**
	 * Load all coments wrt the specified content from DB in an array of Comment object
	 * @param	$content	id of the content
	 */
	public function loadComments($content, $handler) {
		$comments = array();
		$query = "SELECT * FROM COMMENT WHERE content=$content ORDER BY date ASC";
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadComments()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$comments[] = new Comment(
					$row['id'],
					$row['content'],
					$row['author'],
					$row['body'],
					$row['date']
				);
			}
		}
		return $comments;
	}
	
	/**
	 * Load all contents commented recentely
	 */
	public function loadLastCommentedContent($handler) {
		$contents = array();
		$query = "SELECT * FROM COMMENT M, CONTENT N WHERE N.id=M.content ".
			"GROUP BY N.id ORDER BY M.date DESC LIMIT 0,4";
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadLastCommentedContent()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$contents[] = new Content(
					$row['id'],
					$row['type'],
					$row['title'],
					$row['author'],
					$row['body'],
					$row['date']
				);
			}
		}
		print_r($contents);
		return $contents;
	}
	
	/**
	 * Load the content associated to the specified comment id
	 * @param	$id			id of the comment
	 */
	public function loadContentFromCommentId($id, $handler) {
		$content = NULL;
		$query = "SELECT * FROM CONTENT WHERE id=(SELECT content FROM COMMENT WHERE id=$id)";
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadContentFromCommentId()");
		} else if(mysql_num_rows($result)!=1) {
			$handler->writeLog("[SYSTEM ERROR] ".strftime("%c")." 1 result expected but ".mysql_num_rows($result)." retrieved".
				" from Class::Philomousia::loadContentFromCommentId()");
		} else {
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$content = new Content(
				$row['id'],
				$row['type'],
				$row['title'],
				$row['author'],
				$row['body'],
				$row['date']
			);
		}
		return $content;
	}
	
	/**
	 * Insert a comment in DB
	 */
	public function insertComment($content, $author, $body, $handler) {
		$author = $handler->cleanString($author);
		$body = $handler->cleanString($body);
		$query = "INSERT INTO COMMENT(content, author, body) VALUES('$content', '$author', '$body')";
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::insertComment()");
		} else {
			$handler->writeLog("[PANEL] ".strftime("%c")." comment added by ID=".$_SERVER['REMOTE_ADDR']." in post ".$content);
		}
		return $result;	
	}
	
	/**
	 * Get the number of comment wrt the specified content
	 */
	public function getCommentsNumber($handler, $content) {
		$query = "SELECT COUNT(*) AS total FROM COMMENT WHERE content=$content";
		$result = mysql_query($query);
		if(!$result) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::getCommentsNumber()");
			$value = -1;
		} else if(mysql_num_rows($result)!=1) {
			$handler->writeLog("[SYSTEM ERROR] ".strftime("%c")." 1 result expected but ".mysql_num_rows($result)." retrieved".
				" from Class::Philomousia::getContentsNumber()");
			$value = -1;
		} else {
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$value = $row['total'];
		}
		return $value;
		
	}
	
	/**
	 * Delete a specific comment in DB
	 */
	public function deleteComment($id, $handler) {
		$query = "DELETE FROM COMMENT WHERE id=$id";
		$result = mysql_query($query);
		//echo $query;
		if($result) {
			$handler->writeLog("[PANEL] ".strftime("%c")." comment deleted by ".$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
		} else {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::deleteComment()");
		}
		return $result;
	}
	
	
	/*********************************************************************************************************
	 *                                                CONTENT
	 *********************************************************************************************************/
	/**
	 * Load all (faculty=0||thought=1||paper=2)-content from DB with optional filters (in any combination) in
	 * an array of Content objects:
	 * @param	$type			load all contents of type ( 1|2|3 ) ~ ( faculty|thought|paper )
	 * @param	$offset			start loading contents from the specified offset (in date-descending order)
	 * @param	$quantity		number of content to load from the specified offset (if any)
	 * @param	$tag			load all content belonging to the speified tag_id
	 */
	public function loadContents($handler, $type = -1, $offset = -1, $quantity = -1, $tag = -1) {
		$contents = array();
		$where = ($type!=-1 || $tag!=-1)? " WHERE " : "";
		$type_filter = ($type!=-1)? "type='$type'" : "";
		$and = ($type!=-1 && $tag!=-1)? " AND " : "";
		$tag_filter = ($tag!=-1)? "id IN (SELECT content FROM CONTENT_TAG WHERE tag='$tag') ":"";
		$limit = ($offset>=0 && $quantity>=0)? "LIMIT $offset, $quantity" : "";
		$query = "SELECT * FROM CONTENT".$where.$type_filter.$and.$tag_filter.
			"ORDER BY date DESC ".$limit;
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadContents()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$tags = array();
				$query_tag = "SELECT T.id, T.name FROM CONTENT_TAG CT, TAG T WHERE CT.content=".$row['id']." AND CT.tag=T.id";
				if(!($result_tag = mysql_query($query_tag))) {
					$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadContents()");
				} else {
					while($row_tag = mysql_fetch_array($result_tag, MYSQL_ASSOC)) {
						$tags[] = new Tag($row_tag['id'], $row_tag['name']);
					}
				}
				$contents[] = new Content(
					$row['id'],
					$row['type'],
					$row['title'],
					$row['author'],
					$row['body'],
					$row['date'],
					$tags
				);
			}
		}
		return $contents;
	}
	
	/**
	 * Load a specific content from DB in a Content object
	 */
	public function loadContent($id, $handler) {
		$query = "SELECT * FROM CONTENT WHERE id=$id";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadContent()");
		} else if(mysql_num_rows($result)!=1) {
			$handler->writeLog("[SYSTEM ERROR] ".strftime("%c")." 1 result expected but ".mysql_num_rows($result)." retrieved".
				" from Class::Philomousia::loadContent()");
		} else {
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$tags = array();
			$query_tag = "SELECT T.id, T.name FROM CONTENT_TAG CT, TAG T WHERE CT.content=".$row['id']." AND CT.tag=T.id";
			if(!($result_tag = mysql_query($query_tag))) {
				$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadContent()");
			} else {
				while($row_tag = mysql_fetch_array($result_tag, MYSQL_ASSOC)) {
					$tags[] = new Tag($row_tag['id'], $row_tag['name']);
				}
			}
			$content = new Content(
				$row['id'],
				$row['type'],
				$row['title'],
				$row['author'],
				$row['body'],
				$row['date'],
				$tags
			);
		}
		return $content;
	}
	
	/**
	 * Load all contents written in the specified date from DB in an array of Content object
	 */
	public function loadContentsByDate($year, $month, $day, $handler) {
		$contents = array();
		$query = "SELECT * FROM CONTENT WHERE YEAR(date)=$year AND MONTH(date)=$month AND DAY(date)=$day ORDER BY date DESC";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadContentsByDate()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$tags = array();
				$query_tag = "SELECT T.id, T.name FROM CONTENT_TAG CT, TAG T WHERE CT.content=".$row['id']." AND CT.tag=T.id";
				if(!($result_tag = mysql_query($query_tag))) {
					$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadContentsByDate()");
				} else {
					while($row_tag = mysql_fetch_array($result_tag, MYSQL_ASSOC)) {
						$tags[] = new Tag($row_tag['id'], $row_tag['name']);
					}
				}
				$contents[] = new Content(
					$row['id'],
					$row['type'],
					$row['title'],
					$row['author'],
					$row['body'],
					$row['date'],
					$tags
				);
			}
		}
		return $contents;
	}
	
	/**
	 * Retrieve all date in the specified month where has been posted at least an article
	 */
	public function getAllNotEmptyDays($year, $month, $handler) {
		$days = array();
		$query = "SELECT DISTINCT(DAY(date)) as day FROM CONTENT WHERE YEAR(date)=$year AND MONTH(date)=$month";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::getAllNotEmptyDate()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$days[] = $row['day'];
			}
		}
		return $days;
	}
	
	/**
	 * Delete a specific content in DB
	 */
	public function deleteContent($id, $handler) {
		$query_content = "DELETE FROM CONTENT WHERE id=$id";
		$query_content_tag = "DELETE FROM CONTENT_TAG WHERE content=$id";
		$query_comment = "DELETE FROM COMMENT WHERE content=$id";
		$result = mysql_query($query_content) && mysql_query($query_content_tag) && mysql_query($query_comment);
		//echo $query;
		if($result) {
			$handler->writeLog("[PANEL] ".strftime("%c")." content deleted by ".$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
		} else {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::deleteContent()");
		}
		return $result;
	}
	
	/**
	 * Update a specific content in DB
	 */
	public function updateContent($id, $title, $author, $body, $tag, $type, $handler) {
		$title = $handler->cleanString($title);
		$author = $handler->cleanString($author);
		$body = $handler->cleanString($body);
		$result = true;
		$query = "UPDATE CONTENT SET title='$title', author='$author', body='$body', type='$type' WHERE id=$id";
		if(!mysql_query($query)) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::updateContent()");
			$result = false;
		}
		$query = "DELETE FROM CONTENT_TAG WHERE content=$id";
		if(!mysql_query($query)) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::updateContent()");
			$result = false;
		}
		if(count($tag)>0) {
			foreach($tag as $key=>$value) {
				$query = "INSERT INTO CONTENT_TAG VALUES('$id', '$value')";
				if(!mysql_query($query)) {
					$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::updateContent()");
					$result = false;
				}
			}
		}
		if($result) {
			$handler->writeLog("[PANEL] ".strftime("%c")." content modified by ".
				$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
		}
		return $result;
		
	}
	
	/**
	 * Insert content in DB
	 */
	public function insertContent($title, $author, $body, $tag, $type, $handler) {
		$title = $handler->cleanString($title);
		$author = $handler->cleanString($author);
		$body = $handler->cleanString($body);
		$result = true;
		$query = "INSERT INTO CONTENT(title, author, body, type) VALUES('$title', '$author', '$body', '$type')";
		if(!mysql_query($query)) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::insertContent()");
			$result = false;
		}
		$id = mysql_insert_id();
		if(count($tag)>0) {
			foreach($tag as $key=>$value) {
				$query = "INSERT INTO CONTENT_TAG VALUES('$id', '$value')";
				if(!mysql_query($query)) {
					$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::insertContent()");
					$result = false;
				}
			}
		}
		if($result)	{
			$handler->writeLog("[PANEL] ".strftime("%c")." content added by ".$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
		}
		return $result;
		
	}
	
	/**
	 * Get the number of all stored content with an optional type|tag-filter
	 */
	public function getContentsNumber($handler, $type = -1, $tag = -1) {
		$where = ($type!=-1 || $tag!=-1)? " WHERE " : "";
		$type_filter = ($type!=-1)? "type='$type'" : "";
		$and = ($type!=-1 && $tag!=-1)? " AND " : "";
		$tag_filter = ($tag!=-1)? "id IN (SELECT content FROM CONTENT_TAG WHERE tag='$tag') ":"";
		$query = "SELECT COUNT(*) AS total FROM CONTENT".$where.$type_filter.$and.$tag_filter;
		$result = mysql_query($query);
		if(!$result) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::getContentsNumber()");
			$value = -1;
		} else if(mysql_num_rows($result)!=1) {
			$handler->writeLog("[SYSTEM ERROR] ".strftime("%c")." 1 result expected but ".mysql_num_rows($result)." retrieved".
				" from Class::Philomousia::getContentsNumber()");
			$value = -1;
		} else {
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$value = $row['total'];
		}
		return $value;
		
	}
	
	/**
	 * Search for a general content in DB and returns all matching contents in an array of Content objects
	 */
	public function searchContent($content, $handler, $offset = -1, $quantity = -1) {
		$contents = array();
		$limit = ($offset>=0 && $quantity>=0)? "LIMIT $offset, $quantity" : "";
		$query = "SELECT * FROM CONTENT WHERE title LIKE '%$content%' OR body LIKE '%$content%' ".
			"ORDER BY date DESC $limit";
		$query = "SELECT * FROM CONTENT WHERE MATCH(title, body) AGAINST(\"$content\")";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::searchContent()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$contents[] = new Content(
					$row['id'],
					$row['type'],
					$row['title'],
					$row['author'],
					$row['body'],
					$row['date']
				);
			}
		}
		return $contents;
	}
	
	
	/*********************************************************************************************************
	 *                                                TAG
	 *********************************************************************************************************/
	/**
	 * Load all tags from DB in an array of Tag objects
	 */
	public function loadTags($handler) {
		$tags = array();
		$query = "SELECT * FROM TAG";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadTags()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$tags[] = new Tag(
					$row['id'],
					$row['name']
				);
			}
		}
		return $tags;
	}
	
	/**
	 * Load a specific tag from DB in a Tag object
	 */
	public function loadTag($id, $handler) {
		$query = "SELECT * FROM TAG WHERE id=$id";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadTags()");
		} else if(mysql_num_rows($result)!=1) {
			$handler->writeLog("[SYSTEM ERROR] ".strftime("%c")." 1 result expected but ".mysql_num_rows($result)." retrieved ".
				"from Class::Philomousia::loadTag()");
		} else {
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$tag = new Tag(
				$row['id'],
				$row['name']
			);
		
		}
		return $tag;
	}
	
	/**
	 * Add a new tag in DB
	 */
	public function insertTag($name, $handler) {
		$query = "INSERT INTO TAG(name) VALUES('$name')";
		if(($result = mysql_query($query))) {
			$handler->writeLog("[PANEL] ".strftime("%c")." tag ".$_GET['new_tag']." added by ".
				$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
		} else {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::insertTag()");
		}
		return $result;
	}
	
	/**
	 * Modify tags in DB
	 * @param	$post		stores all tuples (id, name)
	 */
	public function manageTags($post, $handler) {
		foreach($post as $key=>$value) {
			$query = "UPDATE TAG SET name='$value' WHERE id=$key";
			if(($result = mysql_query($query))) {
				$handler->writeLog("[PANEL] ".strftime("%c")." tags changed by ".
					$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
			} else {
				$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::manageTags()");
			}
		}
		return $result;
	}
	
	/**
	 * Delete tag in DB
	 */
	public function deleteTag($id, $handler) {
		$query_tag = "DELETE FROM TAG WHERE id=$id";
		$query_content_tag = "DELETE FROM CONTENT_TAG WHERE tag=$id";
		$result = mysql_query($query_tag) && mysql_query($query_content_tag);
		if($result) {
			$handler->writeLog("[PANEL] ".strftime("%c")." tag ".$_GET['id']." deleted by ".
				$handler->getUsername()." ID=".$_SERVER['REMOTE_ADDR']);
		} else {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::deleteTag()");
		}
		return $result;
	}
	
	/**
	 * Get the relative frequence of post per tag in an array of integers
	 */
	public function getTagFreq($handler) {
		$tags = $this->loadTags($handler);
		$freqencies = array();
		$total = 0;
		foreach($tags as $tag) {
			$query = "SELECT COUNT(*) AS total FROM CONTENT_TAG WHERE tag='".$tag->getId()."'";
			$result = mysql_query($query);
			if(!($result = mysql_query($query))) {
				$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::getTagFreq()");
			}
			if(mysql_num_rows($result)!=1) {
				return -1;
			} else {
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				$freqencies[] = (int)$row['total'];
			}
		}
		$posts = $this->getContentsNumber($handler);
		for($i=0; $i<count($freqencies) && $posts!=0; $i++) {
			$freqencies[$i] = $freqencies[$i]/$posts;
		}
		return $freqencies;
	}
	
	
	/*********************************************************************************************************
	 *                                                EDITORIAL
	 *********************************************************************************************************/
	/**
	 * Load last editorial (by date) from DB in an Ediorial object
	 */
	public function loadLastEditorial($handler) {
		$query = "SELECT * FROM EDITORIAL WHERE date=(SELECT MAX(date) FROM EDITORIAL)";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadLastEditorial()");
		} else if(mysql_num_rows($result)>1) {
			$handler->writeLog("[SYSTEM ERROR] ".strftime("%c")." 1 result expected but ".mysql_num_rows($result)." retrieved".
				" from Class::Philomousia::loadLastEditorial()");
		} else if(mysql_num_rows($result)==1) {
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$editorial = new Editorial(
				$row['id'],
				$row['title'],
				$row['author'],
				$row['body'],
				$row['date']
			);
		}
		return $editorial;
	}
	
	/**
	 * Add editorial
	 */
	public function insertEditorial($title, $author, $body, $handler) {
		$title = $handler->cleanString($title);
		$author = $handler->cleanString($author);
		$body = $handler->cleanString($body);
		$query = "INSERT INTO EDITORIAL(title, author, body) VALUES('$title', '$author', '$body')";
		
		if(($result = mysql_query($query))) {
			$handler->writeLog("[PANEL] ".strftime("%c")." editorial added by ".$handler->getUsername()." ID=".
				$_SERVER['REMOTE_ADDR']);
		} else {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::insertEditorial()");
			//echo mysql_error();
		}
		return $result;
	}
	
	
	/*********************************************************************************************************
	 *                                                LINK
	 *********************************************************************************************************/
	/**
	 * Get all links in an array of Links objects
	 */
	public function loadLinks($handler) {
		$links = array();
		$query = "SELECT * FROM LINK";
		//echo $query;
		if(!($result = mysql_query($query))) {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::loadLinks()");
		} else {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$links[] = new Link(
					$row['id'],
					$row['url'],
					$row['description'],
					$row['date']
				);
			}
		}
		return $links;
	}
	
	/**
	 * Add link
	 */
	public function insertLink($url, $description, $handler) {
		$description = $handler->cleanString($description);
		$query = "INSERT INTO LINK(url, description) VALUES('$url', '$description')";
		if(($result = mysql_query($query))) {
			$handler->writeLog("[PANEL] ".strftime("%c")." link added by ".$handler->getUsername()." ID=".
				$_SERVER['REMOTE_ADDR']);
		} else {
			$handler->writeLog("[MYSQL ERROR] ".strftime("%c")." ".mysql_error()." from Class::Philomousia::insertLink()");
		}
		return $result;
	}
	
}
?>